package com.utcs.modules.resource.basic.controller;

import com.utcs.common.persistence.Page;
import com.utcs.common.utils.StringUtils;
import com.utcs.common.web.BaseController;
import com.utcs.modules.resource.basic.entity.Department;
import com.utcs.modules.resource.basic.entity.User;
import com.utcs.modules.resource.basic.service.DepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Fly on 2017/5/23.
 * 部门Controller
 */
@Controller
@RequestMapping("resource/basic/department")
public class DepartmentController extends BaseController {

    @Autowired
    DepartmentService departmentService;

    @ModelAttribute("department")
    public Department get(@RequestParam(required=false) Integer id) {
        if (id!=null){
            return departmentService.get(id);
        }else{
            return new Department();
        }
    }


    /*@RequiresPermissions("basic:department:view")
    @RequestMapping(value = {"/list",""})
    public String list(Model model) {
        model.addAttribute("list", departmentService.findAll());
        return "modules/resource/basic/department";
    }*/

    @RequiresPermissions("basic:department:view")
    @RequestMapping(value = {"/list",""})
    public String list(Model model, Department department, HttpServletRequest request, HttpServletResponse response) {
        Page<Department> page = departmentService.findList(new Page<Department>(request,response),department);
        model.addAttribute("page", page);
        return "modules/resource/basic/department";
    }

    @RequiresPermissions("basic:department:edit")
    @RequestMapping(value = "/form")
    public String form(Department department, Model model) {
        if (department != null) {

        }
        model.addAttribute("department",department);
        return "modules/resource/basic/departmentForm";
    }

    @RequiresPermissions("basic:department:edit")
    @RequestMapping(value = "/save")
    public String save(Department department, Model model, RedirectAttributes redirectAttributes) {
        logger.debug("department:" + department);
        if (!beanValidator(model, department)) {
            return form(department, model);
        }
        departmentService.save(department);
        addMessage(redirectAttributes, "保存部门'" + department.getName() + "'成功");
        return "redirect:/resource/basic/department/list";
    }

    @RequiresPermissions("basic:department:edit")
    @RequestMapping(value = "/delete")
    public String delete(Department department, RedirectAttributes redirectAttributes){
        departmentService.delete(department);
        addMessage(redirectAttributes, "删除部门'" + department.getName() + "'成功");
        return "redirect:/resource/basic/department/list";
    }


    @RequestMapping(value = "/treeData")
    public @ResponseBody
    List<Map<String, Object>> treeData() {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        List<Department> list = departmentService.findAll();
        for (int i = 0; i < list.size(); i++) {
            Department e = list.get(i);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", e.getId());
            map.put("pId", e.getParentId());
            map.put("name", e.getName());
            mapList.add(map);
        }
        return mapList;
    }
}
